package a.location;

import java.util.Iterator;
import java.util.List;

import com.google.android.maps.GeoPoint;
import com.google.android.maps.OverlayItem;

public class LocationUtil {

    public static GeoPoint getCenterPoint(List<GeoPoint> locations) {

        int northEdge = -90000000; // i.e., -90E6 microdegrees
        int southEdge = 90000000;
        int eastEdge = -180000000;
        int westEdge = 180000000;

        Iterator<GeoPoint> iter = locations.iterator();

        while (iter.hasNext()) {
            GeoPoint pt = iter.next();
            if (pt.getLatitudeE6() > northEdge) {
                northEdge = pt.getLatitudeE6();
            }
            if (pt.getLatitudeE6() < southEdge) {
                southEdge = pt.getLatitudeE6();
            }
            if (pt.getLongitudeE6() > eastEdge) {
                eastEdge = pt.getLongitudeE6();
            }
            if (pt.getLongitudeE6() < westEdge) {
                westEdge = pt.getLongitudeE6();
            }
        }

        GeoPoint center = new GeoPoint(
                    (int) ((northEdge + southEdge) / 2),
                    (int) ((westEdge + eastEdge) / 2));
        return center;
    }

    public static GeoPoint getCenterPoint(List<OverlayItem> locations, int dummy) {

        int northEdge = -90000000; // i.e., -90E6 microdegrees
        int southEdge = 90000000;
        int eastEdge = -180000000;
        int westEdge = 180000000;
        Iterator<OverlayItem> iter = locations.iterator();
        while (iter.hasNext()) {
            GeoPoint pt = iter.next().getPoint();
            if (pt.getLatitudeE6() > northEdge)
                northEdge = pt.getLatitudeE6();
            if (pt.getLatitudeE6() < southEdge)
                southEdge = pt.getLatitudeE6();
            if (pt.getLongitudeE6() > eastEdge)
                eastEdge = pt.getLongitudeE6();
            if (pt.getLongitudeE6() < westEdge)
                westEdge = pt.getLongitudeE6();
        }
        GeoPoint center = new GeoPoint(
                    (int) ((northEdge + southEdge) / 2),
                    (int) ((westEdge + eastEdge) / 2));
        return center;
    }
}
